package gui;

import helden.plugin.werteplugin2.PluginHeld2;

import java.awt.Dimension;
import javax.swing.JLabel;
import javax.swing.JPanel;
import java.awt.GridBagLayout;
import java.awt.GridBagConstraints;
import javax.swing.JButton;

import main.Controller;

import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JComboBox;
import javax.swing.JTextArea;

/**
 * 
 * @author Christian
 * @since 0.15d
 */
public class HeldenUpdateZauber extends JPanel {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * 
	 */
	private JPanel jPanel = null;
	
	/**
	 * 
	 */
	private JLabel jZauberNameLabel = null;
	
	/**
	 * 
	 */
	private JPanel jButtonPanel = null;
	
	/**
	 * 
	 */
	private JButton jZurueckButton = null;
	
	/**
	 * 
	 */
	private JButton jWeiterButton = null;
	
	/**
	 * 
	 */
	private int index = 0;
	
	/**
	 * 
	 */
	private PluginHeld2 held = null;
	
	/**
	 * 
	 */
	private JComboBox jVariantenAuswahlComboBox = null;
	
	/**
	 * 
	 */
	private JButton jHauptmenuButton = null;
	
	/**
	 * 
	 */
	private HeldenUpdateController heldenController = null;
	
	/**
	 * 
	 */
	private JTextArea jKommentarTextArea = null;
	
	/**
	 * 
	 */
	private JLabel jRepLabel = null;
	
	/**
	 * 
	 */
	private JButton jAusDemHeldenToolLadenButton = null;
	
	/**
	 * 
	 */
	private JTextArea jKostenTextArea = null;
	
	/**
	 * 
	 */
	private JTextArea jReichweiteTextArea = null;
	
	/**
	 * 
	 */
	private JTextArea jZauberDauerTextArea = null;
	
	/**
	 * 
	 */
	private JTextArea jWirkungsDauerTextArea = null;
	
	/**
	 * 
	 */
	private JTextArea jAnmerkungTextArea = null;
	
	/**
	 * 
	 */
	private JLabel jLabel1 = null;
	
	/**
	 * 
	 */
	private JLabel jLabel2 = null;
	
	/**
	 * 
	 */
	private JLabel jLabel3 = null;
	
	/**
	 * 
	 */
	private JLabel jLabel4 = null;
	
	/**
	 * 
	 */
	private JLabel jLabel5 = null;
	
	/**
	 * 
	 */
	private JLabel jLabel6 = null;
	
	/**
	 * 
	 */
	private JPanel jPanel1 = null;
	
	/**
	 * 
	 * @param held Der ausgewählte Held
	 * @param zauberIndex Index des aktuellen Zaubers
	 */
	public HeldenUpdateZauber(PluginHeld2 held, int zauberIndex) {

		super();
		this.held = held;
		this.index = zauberIndex;
		if (zauberIndex == -1) {
			fertig();
		} else {
			heldenController = new HeldenUpdateController(this, held, zauberIndex);
			initialize();
			heldenController.update();
			heldenController.reset();
		}
	}
	
	/**
	 * This method initializes this
	 * 
	 */
	private void initialize() {

		jZauberNameLabel = new JLabel();
		jZauberNameLabel.setText("ZauberName");
		GridBagConstraints gridBagConstraints23 = new GridBagConstraints();
		gridBagConstraints23.gridx = 0;
		gridBagConstraints23.gridwidth = 11;
		gridBagConstraints23.gridy = 0;
		GridBagConstraints gridBagConstraints22 = new GridBagConstraints();
		gridBagConstraints22.gridx = 0;
		gridBagConstraints22.gridy = 8;
		jLabel6 = new JLabel();
		jLabel6.setText("Zauberdauer:");
		GridBagConstraints gridBagConstraints21 = new GridBagConstraints();
		gridBagConstraints21.gridx = 0;
		gridBagConstraints21.gridy = 9;
		jLabel5 = new JLabel();
		jLabel5.setText("Anmerkung:");
		GridBagConstraints gridBagConstraints20 = new GridBagConstraints();
		gridBagConstraints20.gridx = 0;
		gridBagConstraints20.gridy = 10;
		jLabel4 = new JLabel();
		jLabel4.setText("Kommentar:");
		GridBagConstraints gridBagConstraints19 = new GridBagConstraints();
		gridBagConstraints19.gridx = 0;
		gridBagConstraints19.gridy = 7;
		jLabel3 = new JLabel();
		jLabel3.setText("Wirkungsdauer:");
		GridBagConstraints gridBagConstraints18 = new GridBagConstraints();
		gridBagConstraints18.gridx = 0;
		gridBagConstraints18.gridy = 5;
		jLabel2 = new JLabel();
		jLabel2.setText("Reichweite:");
		GridBagConstraints gridBagConstraints17 = new GridBagConstraints();
		gridBagConstraints17.gridx = 0;
		gridBagConstraints17.gridy = 3;
		jLabel1 = new JLabel();
		jLabel1.setText("Kosten:");
		GridBagConstraints gridBagConstraints16 = new GridBagConstraints();
		gridBagConstraints16.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints16.gridy = 9;
		gridBagConstraints16.weightx = 1.0;
		gridBagConstraints16.weighty = 1.0;
		gridBagConstraints16.insets = new Insets(5, 10, 5, 5);
		gridBagConstraints16.gridwidth = 4;
		gridBagConstraints16.gridx = 1;
		GridBagConstraints gridBagConstraints15 = new GridBagConstraints();
		gridBagConstraints15.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints15.gridy = 7;
		gridBagConstraints15.insets = new Insets(5, 10, 5, 5);
		gridBagConstraints15.weightx = 1.0;
		gridBagConstraints15.weighty = 1.0;
		gridBagConstraints15.gridwidth = 8;
		gridBagConstraints15.gridx = 1;
		GridBagConstraints gridBagConstraints13 = new GridBagConstraints();
		gridBagConstraints13.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints13.gridy = 8;
		gridBagConstraints13.insets = new Insets(5, 10, 5, 5);
		gridBagConstraints13.weightx = 1.0;
		gridBagConstraints13.weighty = 1.0;
		gridBagConstraints13.gridwidth = 9;
		gridBagConstraints13.gridx = 1;
		GridBagConstraints gridBagConstraints14 = new GridBagConstraints();
		gridBagConstraints14.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints14.gridy = 5;
		gridBagConstraints14.insets = new Insets(5, 10, 5, 5);
		gridBagConstraints14.weightx = 1.0;
		gridBagConstraints14.weighty = 1.0;
		gridBagConstraints14.gridwidth = 7;
		gridBagConstraints14.gridx = 1;
		GridBagConstraints gridBagConstraints12 = new GridBagConstraints();
		gridBagConstraints12.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints12.gridy = 10;
		gridBagConstraints12.weightx = 1.0;
		gridBagConstraints12.insets = new Insets(5, 10, 5, 5);
		gridBagConstraints12.weighty = 1.0;
		gridBagConstraints12.gridwidth = 4;
		gridBagConstraints12.gridx = 1;
		GridBagConstraints gridBagConstraints11 = new GridBagConstraints();
		gridBagConstraints11.fill = GridBagConstraints.HORIZONTAL;
		gridBagConstraints11.gridy = 3;
		gridBagConstraints11.weightx = 1.0;
		gridBagConstraints11.insets = new Insets(5, 10, 5, 5);
		gridBagConstraints11.weighty = 1.0;
		gridBagConstraints11.gridwidth = 3;
		gridBagConstraints11.gridx = 1;
		GridBagConstraints gridBagConstraints9 = new GridBagConstraints();
		gridBagConstraints9.gridx = 0;
		gridBagConstraints9.gridy = 1;
		jRepLabel = new JLabel();
		jRepLabel.setText("Rep");
		GridBagConstraints gridBagConstraints6 = new GridBagConstraints();
		gridBagConstraints6.gridx = 0;
		gridBagConstraints6.gridy = 12;
		gridBagConstraints6.gridwidth = 6;
		gridBagConstraints6.insets = new Insets(5, 5, 5, 5);
		GridBagConstraints gridBagConstraints4 = new GridBagConstraints();
		gridBagConstraints4.gridx = 0;
		gridBagConstraints4.gridy = 11;
		gridBagConstraints4.gridwidth = 5;
		gridBagConstraints4.insets = new Insets(5, 5, 5, 5);
		GridBagConstraints gridBagConstraints3 = new GridBagConstraints();
		gridBagConstraints3.gridy = 1;
		gridBagConstraints3.gridx = 1;
		gridBagConstraints3.gridwidth = 3;
		gridBagConstraints3.insets = new Insets(5, 5, 5, 5);
		this.setLayout(new GridBagLayout());
		this.setSize(new Dimension(437, 334));
		this.add(getJPanel(), gridBagConstraints3);
		this.add(getJButtonPanel(), gridBagConstraints4);
		this.add(getJHauptmenuButton(), gridBagConstraints6);
		this.add(getJAusDemHeldenToolLadenButton(), gridBagConstraints9);
		this.add(getJKostenTextArea(), gridBagConstraints11);
		this.add(getJKommentarTextArea(), gridBagConstraints12);
		this.add(getJReichweiteTextArea(), gridBagConstraints14);
		this.add(getJZauberDauerTextArea(), gridBagConstraints13);
		this.add(getJWirkungsDauerTextArea(), gridBagConstraints15);
		this.add(getJAnmerkungTextArea(), gridBagConstraints16);
		this.add(jLabel1, gridBagConstraints17);
		this.add(jLabel2, gridBagConstraints18);
		this.add(jLabel3, gridBagConstraints19);
		this.add(jLabel4, gridBagConstraints20);
		this.add(jLabel5, gridBagConstraints21);
		this.add(jLabel6, gridBagConstraints22);
		this.add(getJPanel1(), gridBagConstraints23);
	}
	
	/**
	 * 
	 * @return Gibt dieses JPanel zurück
	 */
	public JPanel getThisJPanel() {

		return this;
	}
	
	/**
	 * Wird gezeigt wenn man alle Zauber durchlaufen hat.
	 */
	private void fertig() {

		JPanel jFertigPanel = new JPanel();
		jFertigPanel.setSize(new Dimension(98, 77));
		jFertigPanel.add(new JLabel("Fertig"));
		JButton button = new JButton("weiter");
		button.setText("weiter");
		jFertigPanel.add(button, null);
		button.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {

				Controller.getGuiController().zeigeHauptfenster();
			}
		});
		add(jFertigPanel);
	}
	
	/**
	 * @return the jZauberNameLabel
	 */
	public JLabel getJZauberNameLabel() {

		return jZauberNameLabel;
	}
	
	/**
	 * @return the jRepLabel
	 */
	public JLabel getJRepLabel() {

		return jRepLabel;
	}
	
	/**
	 * This method initializes jPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel() {

		if (jPanel == null) {
			GridBagConstraints gridBagConstraints10 = new GridBagConstraints();
			gridBagConstraints10.fill = GridBagConstraints.VERTICAL;
			gridBagConstraints10.gridy = 1;
			gridBagConstraints10.weightx = 1.0;
			gridBagConstraints10.gridx = 0;
			gridBagConstraints10.insets = new Insets(5, 5, 5, 5);
			GridBagConstraints gridBagConstraints = new GridBagConstraints();
			gridBagConstraints.gridx = 0;
			gridBagConstraints.gridy = 0;
			jPanel = new JPanel();
			jPanel.setLayout(new GridBagLayout());
			jPanel.add(getJVariantenAuswahlComboBox(), gridBagConstraints10);
		}
		return jPanel;
	}
	
	/**
	 * This method initializes jButtonPanel
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJButtonPanel() {

		if (jButtonPanel == null) {
			GridBagConstraints gridBagConstraints2 = new GridBagConstraints();
			gridBagConstraints2.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints2.gridy = 2;
			gridBagConstraints2.gridx = 1;
			GridBagConstraints gridBagConstraints1 = new GridBagConstraints();
			gridBagConstraints1.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints1.gridy = 2;
			gridBagConstraints1.gridx = 0;
			jButtonPanel = new JPanel();
			jButtonPanel.setLayout(new GridBagLayout());
			jButtonPanel.add(getJZurueckButton(), gridBagConstraints1);
			jButtonPanel.add(getJWeiterButton(), gridBagConstraints2);
		}
		return jButtonPanel;
	}
	
	/**
	 * This method initializes jZurueckButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJZurueckButton() {

		if (jZurueckButton == null) {
			jZurueckButton = new JButton();
			jZurueckButton.setText("zurück");
			jZurueckButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {

					int newIndex = index;
					newIndex--;
					if (newIndex < 0) {
						Controller.getGuiController().zeigeHeldenUpdatePanel();
					} else {
						Controller.getGuiController().zeigeHeldenUpdateZauber(held, newIndex);
					}
				}
			});
		}
		return jZurueckButton;
	}
	
	/**
	 * This method initializes jWeiterButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJWeiterButton() {

		if (jWeiterButton == null) {
			jWeiterButton = new JButton();
			jWeiterButton.setText("speichern & weiter");
			jWeiterButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {

					heldenController.save();
					int newIndex = index;
					newIndex++;
					if (newIndex >= Controller.getDatenController().getAlleZauberByHeldenId(
							held.getHeldenID()).size()) {
						newIndex = -1;
					}
					Controller.getGuiController().zeigeHeldenUpdateZauber(held, newIndex);
				}
			});
		}
		return jWeiterButton;
	}
	
	
//	private void update() {
//
//		AktiverZauber zauber = Controller.getDatenController().getAlleZauberByHeldenId(
//				held.getHeldenID()).get(index);
//		jZauberNameLabel.setText(zauber.getZauber().getZauberName());
//	}
	
	/**
	 * This method initializes jVariantenAuswahlComboBox
	 * 
	 * @return javax.swing.JComboBox
	 */
	public JComboBox getJVariantenAuswahlComboBox() {

		if (jVariantenAuswahlComboBox == null) {
			jVariantenAuswahlComboBox = new JComboBox();
			jVariantenAuswahlComboBox.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {

					heldenController.update();
				}
			});
			heldenController.fillVariantenAuswahl();
		}
		return jVariantenAuswahlComboBox;
	}
	
	/**
	 * This method initializes jHauptmenuButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJHauptmenuButton() {

		if (jHauptmenuButton == null) {
			jHauptmenuButton = new JButton();
			jHauptmenuButton.setText("zum Hauptmenü");
			jHauptmenuButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {

					Controller.getGuiController().zeigeHauptfenster();
				}
			});
		}
		return jHauptmenuButton;
	}
	
	/**
	 * This method initializes jKommentarTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	public JTextArea getJKommentarTextArea() {

		if (jKommentarTextArea == null) {
			jKommentarTextArea = new JTextArea();
		}
		return jKommentarTextArea;
	}
	
	/**
	 * This method initializes jAusDemHeldenToolLadenButton
	 * 
	 * @return javax.swing.JButton
	 */
	private JButton getJAusDemHeldenToolLadenButton() {

		if (jAusDemHeldenToolLadenButton == null) {
			jAusDemHeldenToolLadenButton = new JButton();
			jAusDemHeldenToolLadenButton.setText("reset");
			jAusDemHeldenToolLadenButton.addActionListener(new ActionListener() {
				
				@Override
				public void actionPerformed(ActionEvent e) {

					heldenController.reset();
				}
			});
		}
		return jAusDemHeldenToolLadenButton;
	}
	
	/**
	 * This method initializes jKostenTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	public JTextArea getJKostenTextArea() {

		if (jKostenTextArea == null) {
			jKostenTextArea = new JTextArea();
		}
		return jKostenTextArea;
	}
	
	/**
	 * This method initializes jReichweiteTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	public JTextArea getJReichweiteTextArea() {

		if (jReichweiteTextArea == null) {
			jReichweiteTextArea = new JTextArea();
		}
		return jReichweiteTextArea;
	}
	
	/**
	 * This method initializes jZauberDauerTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	public JTextArea getJZauberDauerTextArea() {

		if (jZauberDauerTextArea == null) {
			jZauberDauerTextArea = new JTextArea();
		}
		return jZauberDauerTextArea;
	}
	
	/**
	 * This method initializes jWirkungsDauerTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	public JTextArea getJWirkungsDauerTextArea() {

		if (jWirkungsDauerTextArea == null) {
			jWirkungsDauerTextArea = new JTextArea();
		}
		return jWirkungsDauerTextArea;
	}
	
	/**
	 * This method initializes jAnmerkungTextArea
	 * 
	 * @return javax.swing.JTextArea
	 */
	public JTextArea getJAnmerkungTextArea() {

		if (jAnmerkungTextArea == null) {
			jAnmerkungTextArea = new JTextArea();
		}
		return jAnmerkungTextArea;
	}
	
	/**
	 * This method initializes jPanel1
	 * 
	 * @return javax.swing.JPanel
	 */
	private JPanel getJPanel1() {

		if (jPanel1 == null) {
			GridBagConstraints gridBagConstraints8 = new GridBagConstraints();
			gridBagConstraints8.gridx = 3;
			gridBagConstraints8.gridy = 0;
			GridBagConstraints gridBagConstraints5 = new GridBagConstraints();
			gridBagConstraints5.insets = new Insets(5, 5, 5, 5);
			gridBagConstraints5.gridx = 0;
			gridBagConstraints5.gridy = 0;
			gridBagConstraints5.gridwidth = 3;
			jPanel1 = new JPanel();
			jPanel1.setLayout(new GridBagLayout());
			jPanel1.add(jZauberNameLabel, gridBagConstraints5);
			jPanel1.add(jRepLabel, gridBagConstraints8);
		}
		return jPanel1;
	}
} // @jve:decl-index=0:visual-constraint="167,20"
